﻿using blackhouse.BlackApart.Business.Furnitures;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.BlackApart.Business.Apartments {
    class ImpApartmentGroup : IApartmentGroup {

        #region Fields

        private IApartmentLocation location;
        private string shortName = "";
        private IFurniture[] furnitures;

        #endregion

        #region Properties

        public int Id { get; set; }
        public string Name { get; set; }
        public string LidDescription { get; set; }
        public string Description { get; set; }
        public string ShortName {
            get { return string.IsNullOrEmpty(this.shortName) ? this.Name : this.shortName; }
            set { this.shortName = value; }
        }
        public int NumberOfPeople { get; set; }
        public IApartmentLocation DefaultLocalization {
            get {
                if (this.location == null)
                    this.location = Factory.Instance().GetApartmentLocation();
                return this.location;
            }
            set { this.location = value; }
        }
        public float AreaSize { get; set; }

        public double Popularity { get; set; }

        public float Stars { get; set; }

        public Dictionary<string, object> ExtraProperties { get; set; }

        public Seo Seo { get; set; }

        public IApartmentPhoto MainPhoto { get; set; }

        public DateTime LastMod { get; set; }

        public ApartmentOptionField[] Fields { get; set; }

        public string Url { get; set; }

        public Guid Guid { get; set; }

        #endregion

        #region Methods

        public bool Save() {
            return this.Valid();
        }

        private bool Valid() {
            return
                !string.IsNullOrEmpty(this.Name)
                && this.NumberOfPeople > 0
                && this.AreaSize > 0;
        }

        public bool Delete() {
            if (this.Id > 0) {
                this.Id = 0;
                this.Name = "";
                this.NumberOfPeople = 0;
                this.DefaultLocalization = new ImpApartmentLocation();
                this.AreaSize = 0;
                return true;
            }
            else
                return false;
        }

        public bool Exists() {
            return this.Id > 0;
        }

        public IApartment MakeDefaultApartment() {
            IApartment apartment = Factory.Instance().GetApartment();
            apartment.AreaSize = this.AreaSize;
            apartment.Group = this;
            apartment.Location = Factory.Instance().GetApartmentLocation(this.DefaultLocalization.Id);
            apartment.NumberOfPeople = this.NumberOfPeople;
            return apartment;
        }

        public IApartment[] GetApartments() {
            return Factory.Instance().GetApartment().GetApartmentsByGroup(this.Id);
        }

        public IApartmentGroup[] GetAllApartmentTypes() {
            return new IApartmentGroup[] { this };
        }

        public object GetProperty(string propertyKey) {
            if (this.ExtraProperties != null && this.ExtraProperties.ContainsKey(propertyKey)) {
                return this.ExtraProperties[propertyKey];
            }
            else return "";
        }

        public Dictionary<string, string> GetArrayProperty(string propertyKey) {
            return new Dictionary<string, string>();
        }

        public IApartmentPhoto[] GetAllPhotos() {
            return Factory.Instance().GetApartmentPhoto().GetApartmentTypePhotos(this.Id);
        }

        public IFurniture[] GetFurnitures() {
            if (this.furnitures == null)
                this.furnitures = Factory.Instance().GetFurniture().GetApartmentTypeFurnitures(this.Id);
            return this.furnitures;
        }

        #endregion

        #region Equals Methods

        public override bool Equals(object obj) {
            IApartmentGroup group = obj as IApartmentGroup;
            if (group == null) return false;
            return group.Id == this.Id;
        }

        public override int GetHashCode() {
            return this.Id;
        }

        public override string ToString() {
            return this.Name;
        }

        public Seo GetSeo() {
            return this.Seo;
        }

        #endregion

    }
}
